/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifconnect.model.dao;

import ifconnect.model.entity.Comentario;
import ifconnect.model.entity.Disciplina;
import ifconnect.model.entity.Postagem;
import ifconnect.model.entity.Usuario;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author Fidélis
 */
public class UsuarioDao extends DaoGenerico implements Serializable {

    public UsuarioDao() {
        super();
    }

    public Usuario verificarLogin(String email, String senha) {
        Usuario usuario = null;
        this.getEntityManager().getTransaction().begin();

        try {
            Query q = this.getEntityManager().createQuery("select u from Usuario u where u.email = :email and u.senha = :senha and u.status = 'A'");
            q.setParameter("email", email);
            q.setParameter("senha", senha);
            usuario = (Usuario) q.getSingleResult();
            this.getEntityManager().getTransaction().commit();
            return usuario;
        } catch (Exception ex) {
            ex.printStackTrace();
            this.getEntityManager().getTransaction().rollback();
            return null;
        }
    }

    public List<Usuario> getCadastrosPendentes() {
        List<Usuario> cadastrosPendentes = null;
        this.getEntityManager().getTransaction().begin();

        try {
            Query q = this.getEntityManager().createQuery("select u from Usuario u where u.status = 'P'");
            cadastrosPendentes = (List<Usuario>) q.getResultList();
            this.getEntityManager().getTransaction().commit();
            return cadastrosPendentes;
        } catch (Exception ex) {
            ex.printStackTrace();
            this.getEntityManager().getTransaction().rollback();
            return null;
        }
    }

    public int mudarStatus(String novoStatus, Long id) {
        int certo = 0;
        this.getEntityManager().getTransaction().begin();

        try {
            Query q = this.getEntityManager().createQuery("UPDATE Usuario u SET u.status = :status WHERE u.id = :id");
            q.setParameter("status", novoStatus);
            q.setParameter("id", id);
            certo = q.executeUpdate();
            this.getEntityManager().getTransaction().commit();
            return certo;
        } catch (Exception ex) {
            ex.printStackTrace();
            this.getEntityManager().getTransaction().rollback();
            return certo;
        }
    }

    public List<Usuario> listar() {
        List<Usuario> listaDisciplinas = new ArrayList<Usuario>();
        listaDisciplinas = this.listarTodos(Usuario.class);
        return listaDisciplinas;
    }

    public List<Usuario> getProfessores() {
        List<Usuario> cadastrosPendentes = null;
        this.getEntityManager().getTransaction().begin();

        try {
            Query q = this.getEntityManager().createQuery("select u from Usuario u where u.status = 'A' and u.tipo = 'PF'");
            cadastrosPendentes = (List<Usuario>) q.getResultList();
            this.getEntityManager().getTransaction().commit();
            return cadastrosPendentes;
        } catch (Exception ex) {
            ex.printStackTrace();
            this.getEntityManager().getTransaction().rollback();
            return null;
        }
    }

    public List<Postagem> getPostagemUsuario(Usuario id) {
        List<Postagem> postagens;
        try {
            Query q = this.getEntityManager().createQuery("select p from Postagem p where p.usuario=:id order by p.id desc ");
            q.setParameter("id", id);
            postagens = (List<Postagem>) q.getResultList();
        } catch (Exception ex) {
            postagens = null;
            ex.printStackTrace();
        }
        return postagens;
    }

    public Usuario cadastrarDisciplina(Usuario usuario, Collection<Disciplina> disciplinas){
        this.getEntityManager().getTransaction().begin();

        try {
            Usuario novo = this.getEntityManager().merge(usuario);
            novo.setDisciplinas(disciplinas);
            
            this.getEntityManager().getTransaction().commit();
            return novo;
        } catch (Exception ex) {
            ex.printStackTrace();
            this.getEntityManager().getTransaction().rollback();
            return null;
        }
    }
    
    public List<Comentario> getComentarioPorPostagem(Postagem id){
        List<Comentario> comentarios;
        try{
            Query q = this.getEntityManager().createQuery("select p from Comentario p where p.postagem=:id order by p.id desc ");
            q.setParameter("id", id);
            comentarios = (List<Comentario>) q.getResultList();
        } catch (Exception ex){
            comentarios = null;
            ex.printStackTrace();
        }
        return comentarios;
    }
    
    public List<Comentario> getComentarioPostagem(long post){
        List<Comentario> comentarios;
        try {
            Query q = this.getEntityManager().createQuery("select c from Comentario c where c.postagem=:post ");
            q.setParameter("id", post);
            comentarios = (List<Comentario>) q.getResultList();
        } catch (Exception ex) {
            comentarios = null;
            ex.printStackTrace();
        }
        return comentarios;
    }
}
